<template>
  <div id="main">
    <h2>技术信息</h2>
    <div class="head">
      <el-input v-model="searchTableInfo" placeholder="请输入搜索内容" style="width:240px;float: right;margin:20px;" />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="staffId"
          label="员工编号"
        />

        <el-table-column
          align="center"
          prop="staffName"
          label="员工姓名"
        />

        <el-table-column
          align="center"
          prop="staffHiredate"
          label="入职时间"
        />
        <el-table-column
          align="center"
          prop="staffEdu"
          label="学历"
        />

        <el-table-column
          align="center"
          prop="staffMajor"
          label="专业"
        />

        <!-- <el-table-column
          align="center"
          prop="staffJob"
          label="岗位"
        /> -->

        <el-table-column
          align="center"
          prop="staffDuty"
          label="职务"
        />

        <el-table-column
          align="center"
          prop="staffTitle"
          label="职称"
        />

        <el-table-column
          align="center"
          prop="staffRank"
          label="职级"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen(Scope.row.staffId)">编辑</el-button>
            <!-- <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.staffId, Scope.row.staffName)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog title="修改职业发展信息" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="staff" :model="staff" label-position="top" :rules="rules">

        <!-- <el-form-item prop="staffName" label="员工姓名" :label-width="formLabelWidth"> -->
        <!-- <el-input maxlength="20" v-model="staff.staffName" autocomplete="off" readonly /> -->
        <!-- </el-form-item> -->
        <el-form-item prop="staffName" label="员工姓名" :label-width="formLabelWidth">
          <el-input v-model="staff.staffName" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="staffGraduationtime" label="毕业时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="staff.staffGraduationtime"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item prop="staffSchool" label="毕业学校" :label-width="formLabelWidth">
          <el-input v-model="staff.staffSchool" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="staffEdu" label="学历" :label-width="formLabelWidth">
          <el-select v-model="staff.staffEdu" placeholder="请选择">
            <el-option
              v-for="item in Edu"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="staffMajor" label="专业" :label-width="formLabelWidth">
          <el-input v-model="staff.staffMajor" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="staffHiredate" label="入职时间" :label-width="formLabelWidth">
          <el-date-picker v-model="staff.staffHiredate" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" />
        </el-form-item>
        <!-- <el-form-item prop="staffJob" label="岗位" :label-width="formLabelWidth">
          <el-select v-model="staff.staffJob" placeholder="请选择">
            <el-option
              v-for="item in staffJob"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->

        <el-form-item prop="staffDuty" label="职务" :label-width="formLabelWidth">
          <el-input
            v-model="staff.staffDuty"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item prop="staffTitle" label="职称" :label-width="formLabelWidth">
          <el-select v-model="staff.staffTitle" placeholder="请选择">
            <el-option
              v-for="item in staffTitle"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="staffRank" label="职级" :label-width="formLabelWidth">
          <el-select v-model="staff.staffRank" placeholder="请选择">
            <el-option
              v-for="item in staffRank"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          prop="staffJobresume"
          label="工作履历"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffJobresume"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item
          prop="staffProfession"
          label="专业特长"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffProfession"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffTraining"
          label="培训经历"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffTraining"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffWorksummary"
          label="工作总结"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffWorksummary"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffDevelop"
          label="专业发展"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffDevelop"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleUpdate('staff')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAllStaff, getStaffById, updateStaff } from '@/api/staff'
import { validateIdNo, validatePhoneTwo, validateNation, validateEMail, validateName } from '@/api/validata'
export default {
// import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      tableList: [],
      listLoading: false,
      dialogFormVisible: false,
      staff: {},
      newStaff: { },
      formLabelWidth: '200px',
      searchTableInfo: '',
      rules: {
        staffName: [
          { required: true, message: '请输入姓名！', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        staffJurisdiction: [
          { required: true, message: '请选择权限等级！', trigger: 'blur' }
        ],
        staffCall: [
          { required: true, message: '请输入联系电话！', trigger: 'blur' },
          { validator: validatePhoneTwo, trigger: 'blur' }
        ],
        staffIdcard: [
          { required: true, message: '请输入身份证号！', trigger: 'blur' },
          { validator: validateIdNo, trigger: 'blur' }
        ],
        staffEdu: [
          { required: true, message: '请选择权教育程度！', trigger: 'blur' }
        ],
        staffSchool: [
          { required: true, message: '请输入毕业学校！', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        staffMajor: [
          { required: true, message: '请输入员工专业！', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        staffSex: [
          { required: true, message: '请选择员工性别！', trigger: 'blur' }
        ],
        staffBirth: [
          { required: true, message: '请选择员工生日！', trigger: 'blur' }
        ],
        staffNation: [
          { required: true, message: '请输入员工民族！', trigger: 'blur' },
          { validator: validateNation, trigger: 'blur' }
        ],
        staffPolitics: [
          { required: true, message: '请输入员工政治面貌！', trigger: 'blur' }
        ],
        staffResidence: [
          { required: true, message: '请输入员工户口所在地！', trigger: 'blur' }
        ],
        staffLive: [
          { required: true, message: '请输入员工居住地址！', trigger: 'blur' }
        ],
        staffFamily: [
          { required: true, message: '请输入员工家庭成员信息！', trigger: 'blur' }
        ],
        staffContact: [
          { required: true, message: '请输入员工紧急联系方式！', trigger: 'blur' },
          { validator: validatePhoneTwo, trigger: 'blur' }
        ],
        staffGraduationtime: [
          { required: true, message: '请选择员工毕业时间！', trigger: 'blur' }
        ],
        staffEmail: [
          { required: false, message: '请输入正确的邮箱格式！', trigger: 'blur' },
          { validator: validateEMail, trigger: 'blur' }

        ],
        staffHiredate: [
          { required: true, message: '请选择员工入职时间！', trigger: 'blur' }
        ],
        staffDuty: [
          { required: true, message: '请输入员工职务！', trigger: 'blur' }
        ],
        staffTitle: [
          { required: true, message: '请输入员工职称！', trigger: 'blur' }
        ],
        staffRank: [
          { required: true, message: '请输入员工职级！', trigger: 'blur' }
        ],
        staffFatherName: [
          { required: false, message: '请输入正确姓名！', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        staffFatherIdcard: [
          { required: false, message: '请输入身份证号！', trigger: 'blur' },
          { validator: validateIdNo, trigger: 'blur' }
        ],
        staffFatherPhone: [
          { required: false, message: '请输入联系电话！', trigger: 'blur' },
          { validator: validatePhoneTwo, trigger: 'blur' }
        ],
        staffMotherName: [
          { required: false, message: '请输入正确姓名！', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        staffMotherIdcard: [
          { required: false, message: '请输入身份证号！', trigger: 'blur' },
          { validator: validateIdNo, trigger: 'blur' }
        ],
        staffMotherPhone: [
          { required: false, message: '请输入联系电话！', trigger: 'blur' },
          { validator: validatePhoneTwo, trigger: 'blur' }
        ]
      },
      staffJob: [{
        value: '教学',
        label: '教学'
      }, {
        value: '医疗',
        label: '医疗'
      }, {
        value: '人事',
        label: '人事'
      }, {
        value: '财务',
        label: '财务'
      }, {
        value: '后勤',
        label: '后勤'
      }, {
        value: '管理',
        label: '管理'
      }],
      staffTitle: [{
        value: '初级',
        label: '初级'
      }, {
        value: '中级',
        label: '中级'
      }, {
        value: '高级',
        label: '高级'
      }],
      staffRank: [{
        value: '试用期',
        label: '试用期'
      }, {
        value: '1级',
        label: '1级'
      }, {
        value: '2级',
        label: '2级'
      }, {
        value: '3级',
        label: '3级'
      }, {
        value: '4级',
        label: '4级'
      }, {
        value: '5级',
        label: '5级'
      }],
      Edu: [{
        value: '小学',
        label: '小学'
      }, {
        value: '初中',
        label: '初中'
      }, {
        value: '中专',
        label: '中专'
      }, {
        value: '高中',
        label: '高中'
      }, {
        value: '专科',
        label: '专科'
      }, {
        value: '本科',
        label: '本科'
      }, {
        value: '硕士研究生',
        label: '硕士研究生'
      }, {
        value: '博士研究生',
        label: '博士研究生'
      }, {
        value: '博士后',
        label: '博士后'
      }]
    }
  },
  // 监听属性 类似于data概念
  computed: {
    list() {
      const searchTableInfo = this.searchTableInfo
      if (searchTableInfo) {
        return this.tableList.filter(data => {
          return Object.keys(data).some(key => {
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {

    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllStaff().then(data => {
        console.log(data)
        for (var i = 0; i < data.data.records.length; i++) {
          this.newStaff.staffId = data.data.records[i].staffId
          this.newStaff.staffName = data.data.records[i].staffName
          this.newStaff.staffHiredate = data.data.records[i].staffHiredate
          this.newStaff.staffDuty = data.data.records[i].staffDuty
          this.newStaff.staffTitle = data.data.records[i].staffTitle
          this.newStaff.staffRank = data.data.records[i].staffRank
          this.tableList.push(this.newStaff)
          this.newStaff = {}
        }
        this.tableList = data.data.records
        this.listLoading = false
      })
    },
    handleOpen(id) {
      this.staff = {}
      this.dialogFormVisible = true
      getStaffById(id).then(resp => {
        this.staff = resp.data
      })
    },
    handleClose() {
      this.dialogFormVisible = false
      this.staff = {}
    },
    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateStaff(this.staff).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
